Skip to content

Conversation

aSeriousDeveloper
Copy link
Contributor

@aSeriousDeveloper aSeriousDeveloper commented May 21, 2025

EDIT: imported Carbon, apparently I missed that last time. Sorry 😬

Hi, Another addition to the Audit docblock to help with PHPStan type hinting.

This one is to add the auditable_type and auditable_id columns used for the auditable relation so they can be selcted, plucked, etc. The reason for this is an example below:

Audit::query()->distinct()->pluck('auditable_type');

In PHPStan levels 7 and above, this gives the following error:

 ------ ----------------------------------------------------------------------------- 
  Line   Filament/AuditResource.php                                                   
 ------ ----------------------------------------------------------------------------- 
  241    Parameter #1 $column of method                                               
         Illuminate\Database\Eloquent\Builder<OwenIt\Auditing\Models\Audit>::pluck()  
         expects Illuminate\Contracts\Database\Query\Expression|model property        
         of OwenIt\Auditing\Models\Audit, string given.                               
         🪪 argument.type                                                             
         💡 Type #2 from the union: The given string should be a property of          
            OwenIt\Auditing\Models\Audit, auditable_type given.                       
 ------ -----------------------------------------------------------------------------

Adding the properties to the Audit model resolves this. From looking at the migration stub, the morphable prefix for the auditable column cannot be changed so there shouldn't be a risk of these type hinted properties being incorrect.

User Properties

I haven't made the same change for the user relation. The reason why is that the config (and migration) allows for altering the prefix of the user morph column.

'user' => [
'morph_prefix' => 'user',
'guards' => [
'web',
'api',
],
'resolver' => OwenIt\Auditing\Resolvers\UserResolver::class,
],

$table->bigIncrements('id');
$table->string($morphPrefix . '_type')->nullable();
$table->unsignedBigInteger($morphPrefix . '_id')->nullable();
$table->string('event');

Therefore, trying to type hint them would be unreliable, as they can be changed.

@erikn69 erikn69 merged commit 0f67b52 into owen-it:master May 21, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants